package com.rc.saas.tenant.controller.statistics;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.WebPageInfo;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.controller.fault.RcElecPileFaultRecordController;
import com.rc.saas.tenant.model.fault.RcElecPileFaultRecord;
import com.rc.saas.tenant.search.fault.RcElecPileFaultRecordSearch;
import com.rc.saas.tenant.search.statistics.ChargeStatisticsSearch;
import com.rc.saas.tenant.service.statistics.StatisticsService;
import com.rc.saas.tenant.util.ExportUtils;
import com.rc.saas.tenant.vo.channel.RcChannelApplyVo;
import com.rc.saas.tenant.vo.order.ChargeStatisticsResult;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.Objects;

@Controller
@RequestMapping("/statistics")
public class ChargeStatisticsController {
    private static Logger logger = LogManager.getLogger(ChargeStatisticsController.class);

    @Autowired
    private StatisticsService statisticsService;

    @RequestMapping("/charge/index")
    public String index() {
        return "/templates/statistics/chargeStatistics";
    }

    @ResponseBody
    @RequestMapping("/charge/listPageChargeStatistics")
    public WebPageInfo listPageChargeStatistics(ChargeStatisticsSearch search) {
        PageInfo pageInfo =  statisticsService.listPageChargeStatistics(search);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }

    @RequestMapping("/temp1")
    public String temp1() {
        return "/templates/statistics/temp1";
    }

    @RequestMapping("/temp2")
    public String temp2() {
        return "/templates/statistics/temp2";
    }


    /**
     * 导出充值充电统计列表数据
     */
    @ResponseBody
    @RequestMapping("/charge/exporListPageChargeStatistics")
    public TResponse exporListPageChargeStatistics(ChargeStatisticsSearch search, HttpServletResponse response) {
        response.setHeader("Set-Cookie", "fileDownload=true; path=/");
        if(!Objects.isNull(search)){
            search.setPageSize(1000);
        }
        //导出拼接开始条数
        try {
            PageInfo pageInfo =  statisticsService.listPageChargeStatistics(search);
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("充值充电统计列表列表", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), ChargeStatisticsResult.class).sheet("充值充电统计列表列表").doWrite(pageInfo.getList());
            return TResponse.SUCCESS();
        } catch (Exception e) {
            logger.error("导出失败", e.getMessage());
            return TResponse.FAIL(e.getMessage());
        }
    }

}
